import { createRouter, createWebHistory } from 'vue-router'



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'mfBlog',
      component: () => import('../views/index.vue'),
    },
    {
      path: '/admin',
      name: 'admin',
      component: () => import('../views/backstage/index.vue'),
      meta: { requiresAuth: true } // 标记该路由需要登录
    }
  ]
})


router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('userInfo'); // 假设登录信息存储在 localStorage 中的 'userInfo' 键下

  if (to.matched.some(record => record.meta.requiresAuth) && !isLoggedIn) {
    // 如果要访问的路由需要登录且用户未登录，重定向到登录页面
    next('/');
  } else {
    next();
  }
});

function getCookie(name) {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) return parts.pop().split(';').shift();
}


export default router;
